# This short notebook will show how to take symbolic integrals in Python.
# We will require the 'SymPy' module.
import sympy as sym
# First, let's define a symbol x.
x = sym.Symbol('x')
# Then we can define f in terms of x.
f = 2*x**2+3
# To evaluate f at a particular value of x, we can use 'subs()'.
z = f.subs(x, 2)
print('2(2)^2 + 3 =', z)
2(2)^2 + 3 = 11
# To integrate f with respect to x, we use 'integrate()'.
intf = f.integrate(x)
intf
# We can get the same result using 'integrate()' in the following way:
intf = sym.integrate(f, x)
intf
# The above examples are indefinite integrals of f. We can evaluate
# definite integrals using the same 'integrate()' function. We just
# need to pass the limits of integration...
intf = sym.integrate(f, (x, 0, 3))
intf
# or:
intf = f.integrate((x, 0, 3))
intf
# You can also used symbols for the integration limits.
a = sym.Symbol('a')
b = sym.Symbol('b')
intf = f.integrate((x, a, b))
intf
# We can make our functions more complicated. Here's a function of two variables.
y = sym.Symbol('y')
g = x*sym.sin(x*y)
g
# Here's the x-integral of g...
g.integrate(x)
# and here's the y-integral.
g.integrate(y)
# Notice that the results of both of the g integrals depend on whether or not
# x and y are zero. We can specify that our variables are non-zero when they
# are first declared.
x = sym.Symbol('x', nonzero = True)
y = sym.Symbol('y', nonzero = True)
g = x*sym.sin(x*y)
# Now the solutions of our integrals have no conditions.
g.integrate(x), g.integrate(y)
(-x*cos(x*y)/y + sin(x*y)/y**2, -cos(x*y))
# Alternatively, you can define Python functions.
def fcn(x):
return 2*x**2 + 3
# These functions can be integrated in the same way. Here's the indefinite
# integral...
fcn(x).integrate(x)
# ... and here's the indefinite integral.
sym.integrate(fcn(x), (x, a, b))
# Of course, you can have functions of multiple variables.
def gcn(x, y):
return x*sym.sin(x*y)
# Here's the indefinite y-integral followed by the indefinite x-integral.
z = sym.integrate(sym.integrate(gcn(x, y), y), x)
z
# Here's the definite y-integral followed by the definite x-integral using
# the other method of calling the 'integrate()' function.
z = gcn(x, y).integrate((y, 1, 3)).integrate((x, -2, -1))
print(z)
print(sym.N(z))
-sin(1) + sin(3)/3 - sin(6)/3 + sin(2) 0.208004944104050